package org.example.service;

import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.RpcContext;

/**
 * @Author: tang
 * @Description:
 * @Date 2025/7/29 23:14
 */
@DubboService
public class DemoServiceImpl implements DemoService{

    @Override
    public String sayHello(String name) {
        String nameAttatch = RpcContext.getServerAttachment().getAttachment("name");
        System.out.println("nameAttatch " + nameAttatch);
        RpcContext.getServerContext().setAttachment("name", "tang11");
        return "Hello " + name;
    }

    @Override
    public StreamObserver<String> sayHelloStream(StreamObserver<String> response) {
        return new StreamObserver<String>() {
            @Override
            public void onNext(String data) {
                System.out.println(data);
                response.onNext("Hello " + data);
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {
                System.out.println("ProviderApp onCompleted");
                response.onCompleted();
            }
        };
    }

    @Override
    public void sayHelloServerStream(String request, StreamObserver<String> response) {
        response.onNext("Hello " + request);
        response.onNext("Hello 1 " + request);
        response.onCompleted();
    }

}
